package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.LeaderDao;
import com.example.spark.entity.Leader;
import com.example.spark.entity.dto.PaginationDTO;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.LeaderService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
public class LeaderServiceImpl extends ServiceImpl<LeaderDao, Leader> implements LeaderService {
    @Resource
    MinioCloudStorageService minIoService;

    private final LeaderDao leaderDao;

    public LeaderServiceImpl(LeaderDao leaderDao) {
        this.leaderDao = leaderDao;
    }

    /**
     * 获取负责人
     * * 模糊查询，并分页
     */
    @Override
    public CommonResult<IPage<Leader>> getAll(PaginationDTO<Leader> pageDto) {
        LambdaQueryWrapper<Leader> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(pageDto.getEntity().getName()), Leader::getName, pageDto.getEntity().getName());
        IPage<Leader> page = new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize());
        leaderDao.selectPage(page,lqw);
        if (pageDto.getCurrentPage() > page.getPages()){
            pageDto.setCurrentPage((int) page.getPages());
            return getAll(pageDto);
        }
        return new CommonResult<>(200,"查询成功",page);
    }

    /**
     * 添加负责人
     */
    @Override
    public CommonResult<Leader> saveOne(Leader leader) {
        leader.setPhoto("");
        leaderDao.insert(leader);
        return new CommonResult<>(200,"添加成功",leader);
    }

    private String getPhotoPath(MultipartFile photo, Leader leader) throws IOException {
        String photoExtension = FilenameUtils.getExtension(photo.getOriginalFilename());
        return minIoService.uploadPhotoSuffix(photo.getBytes(),photoExtension, String.valueOf(leader.getId()));
    }

    /**
     * 删除负责人
     */
    @Override
    public CommonResult<Long> deleteById(Long id) {
        leaderDao.deleteById(id);
        return new CommonResult<>(200,"删除成功", id);
    }

    /**
     * 修改负责人
     */
    @Override
    public CommonResult<Leader> updateOne(Leader leader) {
        leaderDao.updateById(leader);
        return new CommonResult<>(200,"修改成功",leader);
    }

    /**
     * 更新图片
     */
    @Override
    public CommonResult<String> updatePhoto(MultipartFile photo, Long id) throws IOException {
        Leader leader = leaderDao.selectById(id);
        if (leader == null) return new CommonResult<>(200,"负责人不存在", null);
        String photoPath = getPhotoPath(photo, leader);
        leader.setPhoto(photoPath);
        leaderDao.updateById(leader);
        return new CommonResult<>(200,"添加成功",leader.getPhoto());
    }

    @Override
    public CommonResult<List<Leader>> getLeaders() {
        List<Leader> leaders = leaderDao.selectList(null);
        return new CommonResult<>(200,"查询成功",leaders);
    }

}
